package cn.qqncn.furniture.service;

import cn.qqncn.furniture.mapper.CarMapper;
import cn.qqncn.furniture.pojo.domain.Car;
import cn.qqncn.furniture.pojo.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zkw
 * @ClassName CarService.java
 * @Description TODO
 * @createTime 2020年12月22日 20:44:00
 */
@Service
public class CarService {
    @Resource
    CarMapper carMapper;


    /**
     * 查询该用户的购物车
     * @param userId
     * @return
     */
    public List<CarVo> getCar(Integer userId){
        List<CarVo> carVos = carMapper.selectAllByUserId(userId);
        return carVos;
    }

    public int deleteCar(Car car){
        int i = carMapper.deleteByPrimaryKey(car);
        return i;
    }

    /**
     * 添加购物车数据
     * 如果该用户已经有这个视频的购物车数据
     * 则增加购物车库存
     * @param cart
     * @return
     */
    public int addCart(Car cart){
        Car car = carMapper.selectByPrimaryKey(cart);
        if(car==null){
            return carMapper.insert(cart);
        }else{
            car.setNumber(car.getNumber()+cart.getNumber());
            return  carMapper.updateByPrimaryKey(car);
        }
    }

    /**
     * 删除该用户的全部购物车
     * @param userId
     * @return
     */
    public int deleteAllByUserId(Integer userId){
        int i = carMapper.deleteAllByUserId(userId);
        return i;
    }
}
